package com.acce.gxsc.page.supervisionHandover;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.netBean.NetSupervisionBean;
import com.acce.gxsc.model.netBean.NetSupervisionListBean;
import com.acce.sdk.BaseActivity;
import com.acce.sdk.base.mvp.BaseMvpFragment;
import com.acce.sdk.widget.adapterrv.CommonAdapter;
import com.acce.sdk.widget.adapterrv.ViewHolder;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by huxley on 2017/5/26.
 */
public class SupervisionHandoverFragment extends BaseMvpFragment<SupervisionHandoverView, SupervisionHandoverPresenter> implements SupervisionHandoverView {

    @Bind(R.id.rv_supervision_handover)
    RecyclerView mRvSupervisionHandover;

    View                              rootView;
    ArrayList<NetSupervisionBean>     mDate;
    CommonAdapter<NetSupervisionBean> mCommonAdapter;
    String                            superviseRemarks;

    @Bind(R.id.tv_total_a)
    TextView mTvTotalA;
    @Bind(R.id.tv_total_b)
    TextView mTvTotalB;
    @Bind(R.id.tv_total_c)
    TextView mTvTotalC;
    @Bind(R.id.tv_total_d)
    TextView mTvTotalD;
    @Bind(R.id.cb)
    CheckBox mCb;

    public static SupervisionHandoverFragment newInstance() {
        Bundle args = new Bundle();
        SupervisionHandoverFragment fragment = new SupervisionHandoverFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected SupervisionHandoverPresenter createPresenter() {
        return new SupervisionHandoverPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_supervision_handover, container, false);
        ButterKnife.bind(this, rootView);
        init();
        setHasOptionsMenu(true);
        mPresenter.loadSupervisionHandover(superviseRemarks);
        return rootView;
    }

    private void init() {

        mDate = new ArrayList<>();
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRvSupervisionHandover.setLayoutManager(linearLayoutManager);
        mRvSupervisionHandover.setAdapter(mCommonAdapter = new CommonAdapter<NetSupervisionBean>(getContext(), R.layout.item_supervision_handover, mDate) {
            @Override
            protected void convert(ViewHolder holder, final NetSupervisionBean supervisionBean, final int position) {
                holder.setText(R.id.tv_id, String.valueOf(position + 1));
                holder.setText(R.id.tv_dilivery_type, Tools.getString(supervisionBean.diliveryType));
                holder.setText(R.id.tv_plate_num, Tools.getString(supervisionBean.plateNum));
                holder.setText(R.id.tv_hang_num, Tools.getString1(supervisionBean.containNo, supervisionBean.hangNum));
                holder.setText(R.id.tv_supervise_remarks, Tools.getString(supervisionBean.superviseRemarks));
                holder.setText(R.id.tv_supervise_time, Tools.getTime2(supervisionBean.superviseTime));
            }
        });

        mCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                superviseRemarks = b ? "1" : null;
                mPresenter.loadSupervisionHandover(superviseRemarks);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void showProgressDialog(String s, boolean b) {
        ((BaseActivity) getActivity()).showProgressDialog(s, b);
    }

    @Override
    public void hideProgressDialog() {
        ((BaseActivity) getActivity()).hideProgressDialog();
    }

    @Override
    public void loadSupervisionHandoverSuccess(NetSupervisionListBean supervisionList) {
        mCommonAdapter.getDatas().clear();
        if (supervisionList.dutyList != null && !supervisionList.dutyList.isEmpty()) {
            mCommonAdapter.getDatas().addAll(supervisionList.dutyList);
        }
        mCommonAdapter.notifyDataSetChanged();

        ArrayList<NetSupervisionListBean.StatInfo> statInfo = supervisionList.statInfo;
        NetSupervisionListBean.StatInfo si;
        if (statInfo == null || statInfo.isEmpty()) {
            si = new NetSupervisionListBean.StatInfo("0", "0", "0", "0", "0", "0", "0", "0");
        } else {
            si = statInfo.get(0);
        }
        mTvTotalA.setText("监察审核：" + Tools.getString(si.totalA) + "台次");
        mTvTotalB.setText("新区内：" + Tools.getString(si.totalB) + "台次");
        mTvTotalC.setText("差异车辆：" + Tools.getString(si.totalC) + "台次");
        mTvTotalD.setText("正确车辆：" + Tools.getString(si.totalD) + "台次");
    }

}
